package com.gitee.wsl.struct.category

object CategoryItemHelper {
    fun buildRoot(root: CategoryItemWarp, itemList: List<I_CategoryItem<*>>) {
        val itemWarpArrayList = mutableListOf<CategoryItemWarp>()
        var warp: CategoryItemWarp
        for (item in itemList) {
            warp = CategoryItemWarp(item)
            itemWarpArrayList.add(warp)
        }
        buildTreeWithItemList(root, itemWarpArrayList)
        root.addSubCategoryItems(itemWarpArrayList as List<CategoryItemWarp>)
    }

    fun buildTreeWithItemList(
        parent: CategoryItemWarp,
        itemWarpArrayList: MutableList<CategoryItemWarp>
    ) {
        for (item in itemWarpArrayList) {
            if ((item.data as I_CategoryItem<*>).parentKey?.equals((parent.data as I_CategoryItem<*>).key) == true)
                parent.addSubCategoryItem(item )
        }
        val childItemList: List<CategoryItemWarp> = parent.subItemList
        itemWarpArrayList.removeAll(childItemList.toSet())
        for (item in childItemList) {
            buildTreeWithItemList(item , itemWarpArrayList)
        }
    }
}